package za.co.chaotik.tron;

import java.util.HashMap;
import java.util.Map;

import za.co.chaotik.tron.bots.RandomMoveBot;
import za.co.chaotik.tron.exceptions.UnsupportedNumberOfPlayersException;
import za.co.chaotik.tron.maps.SphericalMap;
import za.co.chaotik.tron.screens.GameOverScreen;
import za.co.chaotik.tron.screens.GdxSplashScreen;
import za.co.chaotik.tron.screens.MapScreen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;

public class CycleGame extends ScreenTransitionGame {
	private static final String LOG = CycleGame.class.getSimpleName();
	
	private FPSLogger fpsLogger;
	
	private boolean showFPS = true;
	
	public CycleGame() {
		super();
	}
	
	@Override
	public void create() {
		Gdx.app.log(LOG, "Creating application...");
		
		fpsLogger = new FPSLogger();
		
		screens = new HashMap<String, Screen>(4);
		
		// Create map
		//AbstractMap map = new RectangularMap(9, 9, 2);
		AbstractMap map = new SphericalMap(10, 10, 2);
		
		try {
			map.init();
		} catch (UnsupportedNumberOfPlayersException e) {
			Gdx.app.log(LOG, "Unsupported numner of players.", e);
			Gdx.app.exit();
		}
		
		// Add an AI player manually for now
		RandomMoveBot bot = new RandomMoveBot();
		map.getPlayers().get(1).setAI(bot);
		bot.setMap(map);
		bot.setPlayer(map.getPlayers().get(1));
		
		// Test with 2 AI players
		//bot = new RandomMoveBot();
		//map.getPlayers().get(0).setAI(bot);
		//bot.setMap(map);
		//bot.setPlayer(map.getPlayers().get(0));
		
		// Create screens
		this.addScreen("GdxSplash", new GdxSplashScreen(this));
		MapScreen mapScreen = new MapScreen(this, map);
		this.addScreen("MapScreen", mapScreen);
		this.addScreen("GameOver", new GameOverScreen(this, map));
		
		// Set the starting screen
		//showScreen("GdxSplash");
		this.showScreen("GdxSplash");
		
		Gdx.app.log(LOG, "GDX Version: " + Gdx.app.getVersion());
	}

	@Override
	public void dispose() {
		Gdx.app.log(LOG, "Disposing application...");
		
		for (String key : screens.keySet()) {
			screens.get(key).dispose();
		}
	}

	@Override
	public void render() {
		// Clear the screen to black
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		// Delegate rendering to active screen
		super.render();

		// Log FPS if enabled
		if (showFPS) {
			fpsLogger.log();
		}
	}

	@Override
	public void resize(int width, int height) {
		Gdx.app.log(LOG, "Resizing application to (" + width + ", " + height + ")...");
		
		super.resize(width, height);
	}

	@Override
	public void pause() {
		Gdx.app.log(LOG, "Pausing application...");
		
		super.pause();
	}

	@Override
	public void resume() {
		Gdx.app.log(LOG, "Resuming application...");

		super.resume();
	}
	
	public boolean isShowFPS() {
		return showFPS;
	}

	public void setShowFPS(boolean showFPS) {
		this.showFPS = showFPS;
	}
}
